//
//  YYMineView.m
//  Project
//
//  Created by Yown on 2024/2/5.
//

#import "YYMineView.h"
#import "YYMineCell.h"

#import <BIProjectModule/BIProjectContext.h>
#import <BIToolModule/BIProjectDefine.h>
#import <BIRouteModule/BIRouteManager.h>
#import <BIProjectModule/BITableView.h>

#import <BIProjectModule/BIAppDefine.h>
#import <BIToolModule/UIView+Custom.h>
#import <BIToolModule/NSArray+Helper.h>
#import <BIToolModule/NSString+Network.h>

#import <SDWebImage/SDWebImage.h>

@interface YYMineView () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIImageView *avatarImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *idLabel;
@property (weak, nonatomic) IBOutlet UILabel *toLoginLabel;
@property (weak, nonatomic) IBOutlet UIImageView *toLoginImageView;

@property (weak, nonatomic) IBOutlet BITableView *tableView;
@property (nonatomic, copy) NSArray *cellArray;


@end

@implementation YYMineView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.backgroundColor = [UIColor loadColorFromString:@"#F6F6F6"];
    
    self.headerView.userInteractionEnabled = YES;
    self.headerView.backgroundColor = [UIColor clearColor];
//    [self.headerView configCorners:UIRectCornerTopLeft | UIRectCornerTopRight radius:20 size:CGSizeMake(BICalculate(331), 100)];
    [self.headerView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickHeaderView)]];
    
    self.avatarImageView.contentMode = UIViewContentModeScaleToFill;
    self.avatarImageView.layer.masksToBounds = YES;
    self.avatarImageView.layer.cornerRadius = 30;
    self.nameLabel.textColor = [UIColor loadColorFromString:@"#1C2023"];
    self.nameLabel.font = BIFont(18);
    self.idLabel.textColor = [UIColor loadColorFromString:@"#666666"];
    self.idLabel.font = BIFont(18);
    
    self.toLoginLabel.textColor = [UIColor loadColorFromString:@"#828282"];
    self.toLoginLabel.font = BIFont(18);
    self.toLoginLabel.text = BILocalizedString(@"mine_login");
//    self.toLoginImageView.image = [UIImage imageNamed:@"mine_arrow"];
    
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.bounces = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"YYMineCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:[YYMineCell cellIdentifier]];
}

- (void)reloadView:(NSArray *)cellArray {
    self.cellArray = cellArray;
    [self.tableView reloadData];
    
    BIUserModel *user = [BIProjectContext sharedInstance].user;
    
    self.toLoginLabel.hidden = [user isLogin];
    self.toLoginImageView.hidden = [user isLogin];
    self.nameLabel.hidden = ![user isLogin];
    self.idLabel.hidden = ![user isLogin];
    
    if ([user isLogin]) {
        [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:user.avatar] placeholderImage:[UIImage imageNamed:@"mine_avatar_placeholder"]];
        self.nameLabel.text = user.phone;
        self.idLabel.text = [NSString stringWithFormat:@"id：%@", user.customerId];
    } else {
        self.avatarImageView.image = [UIImage imageNamed:@"mine_avatar_placeholder"];
        self.toLoginLabel.text = BILocalizedString(@"mine_login");
    }
}

#pragma mark - Action

- (void)clickHeaderView {
    BIUserModel *user = [BIProjectContext sharedInstance].user;
    if ([user isLogin]) {
        [[BIRouteManager sharedInstance] openVCCMD:@"mine_info" isPresent:NO arguments:nil];
    } else {
        [[BIProjectContext sharedInstance] openLoginVCWithSucceedBlock:nil];
    }
}

#pragma mark - UITableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cellArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [YYMineCell cellHeight];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YYMineCellModel *model = [self.cellArray safeObjectAtIndex:indexPath.row];
    YYMineCell *cell = [tableView dequeueReusableCellWithIdentifier:[YYMineCell cellIdentifier] forIndexPath:indexPath];
    [cell setupModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YYMineCellModel *model = [self.cellArray safeObjectAtIndex:indexPath.row];
    if (model.isCheckLogin) {
        if ([[BIProjectContext sharedInstance].user isLogin]) {
            [self jumpToVC:model.jumpCmd];
        } else {
            [[BIProjectContext sharedInstance] openLoginVCWithSucceedBlock:nil];
        }
    } else {
        [self jumpToVC:model.jumpCmd];
    }
}

- (void)jumpToVC:(NSString *)cmd {
    if ([NSString isURL:cmd]) {
        [[BIRouteManager sharedInstance] openVCURL:cmd isPresent:NO arguments:nil];
    } else {
        [[BIRouteManager sharedInstance] openVCCMD:cmd isPresent:NO arguments:nil];
    }
}

@end
